<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php';
require_once 'CMS/User.php';

//Người tạo : Nguyễn Hồng Danh - Team 2
//Module 	: DescriptionNews
//Chức năng	: Show ra mô tả của tin tức
 
class ProductDetail_ProductDetailController extends BaseUserController{

	public function init(){
		parent::init();
	}

	public function addcartAction()
	{
		//-----------------------Khai báo các parameter----------------------
		$tbThings= new ProductDetail_Model_Things();
		
		$dateInt = new Util();
		$access=false;
		//---------------------- Khai báo các parameter----------------------//
		
		
		//-----------------------Check Input Parameter-----------------------
		if(count($_GET) && isset($_GET["AddCart"]) && $dateInt->is_interger($_GET["AddCart"]))
		{
			//Check thing_id có trong db?
			$ProductDetail = $tbThings->getThingById($_GET["AddCart"]);
			if(count($ProductDetail)>0)
				$access=true;
		}
		if(!$access) 
		{
			$this->_redirect('/index/index');
		}
		//-----------------------Check Input Parameter------------------------//
		
		
		//-----------------------Xử lý add to cart----------------------------
		
		{
			$productId=$_GET["AddCart"];
			$quantity=1;
			if(isset($_SESSION["Cart"][$productId]))//Sản phẩm đã thêm trước đó, thì tăng số lượng lên 1
			{
				$quantity=$_SESSION["Cart"][$productId]+1;
			}
			$_SESSION["Cart"][$productId]=$quantity;
			while(list($product, $quan) = each($_SESSION["Cart"]))
			{
				$ProductDetail = $tbThings->getThingById($product);
				$kt=strlen($ProductDetail['title']);
				if ($kt<=14)  
					$response=$response.$ProductDetail["title"]."&nbsp x &nbsp".$quan."<br>";
				else 
				{
					$sub=substr($ProductDetail['title'], 0,13)."..";
					$response=$response.$sub."&nbsp x &nbsp".$quan."<br>";
				}
			}
			$this->_helper->json($response);
		}
		//----------- ----------Xử lý add to Cart------------------------------//
	}
	
	public  function deleteAction() {

		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/productdetail');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
	
		/*========== Start Check Permission ==========*/
		$path = APPLICATION_PATH."/widgets/productdetail/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);
		
		$user = new User();
		$user->init();
		$user->check_login();
		$hasmodify = $user->modifiy_content($xmlconfig->name);
		if($hasmodify=="false")
		{
			$data=$language->get('error100');
			$this->_helper->json($data);
			return;
		}
		/*========== End Check Permission ==========*/
		
		$util= new Util();
		if(isset($_GET["id"])&& $util->is_interger($_GET["id"]) )
		{
			$id=$_GET["id"];
			$data=1;
			$tbthing= new ProductDetail_Model_Things();
			
			$tbComment=new ProductDetail_Model_Comment();
			$tbLike=new ProductDetail_Model_Like();
			$tbImage=new ProductDetail_Model_Imagetothing();
			$tbBillDetail=new ProductDetail_Model_BillDetail();
			
			$result=$tbComment->deletething($id);
			$result=$tbBillDetail->deletething($id);
			$result=$tbImage->deletething($id);
			$result=$tbLike->deletething($id);
			$resust=$tbthing->deletething($id);
			
			if($resust==-1)
			{
				$data=-2;
			}
			
		}
		else {
			$data=-1;
		}
	$this->_helper->json($data); 
		
	}
}